<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>标题</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        [v-cloak] {
            display: none;
        }
        
        .tb {
            width: 100%;
            text-align: center;
            border-collapse: collapse;
        }
        
        .tb.hover-change-color tr:hover {
            background: #eee;
        }
        
        
        
        .tb td,
        .td th {
            padding: 5px 0;
            border: 1px solid #eee;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <input type="text" v-model="name">
        <button @click="search">查找</button>
        <div style="width: 100%;">
            <tb :data="booklist" hover-change-color>
                <td-col label="小说" field="name"></td-col>
                <td-col label="类型" field="type1"></td-col>
                <td-col label="作者" field="author"></td-col>
                <td-col label="更新时间" field="utime"></td-col>
            </tb>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.common.dev.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
    <script>
        Vue.component('td-col', {
            props: {
                label: {
                    type: String
                },
                field: {
                    type: String
                }
            },
            template: `
                <th>{{label}}</th>
            `,
            created() {
                this.$parent.fieldlist.push(this.field)
            },
            data() {
                return {

                }
            },
        })

        Vue.component('tb', {
            props: {
                data: {
                    type: Array,
                },
                hoverChangeColor: {
                    type: Boolean
                }
            },
            data() {
                return {
                    fieldlist: []
                }
            },
            template: `
            <table class="tb" :class="{'hover-change-color':hoverChangeColor}" >
                <tr>
                    <slot></slot>
                </tr>
                <tr v-for="item in data" @click="check(item['href'])">
                    <td v-for="field in fieldlist">{{item[field]}}</td>
                </tr>
            </table>
            `,
            methods: {
                check(href) {
                    window.location = '/index.html'
                }
            }
        })

        let vm = new Vue({
            el: "#app",
            created() {

            },
            methods: {
                search() {
                    axios.get('http://127.0.0.1:10021/api/search?name=' + this.name).then((response) => {
                        this.booklist = response.data

                    }).catch((error) => {
                        console.log(error)
                    })
                }
            },
            mounted() {

            },
            data() {
                return {
                    booklist: [],
                    name: ""
                }
            },
        })
    </script>
</body>

</html>